﻿using Dong.IDAL;
using System.Configuration;
using System.Reflection;
namespace Dong.DALFactory
{
    /// <summary>
    /// 抽象工厂模式创建DAL。
    /// App.config 需要加入配置：(利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口) 
    /// DataCache类在导出代码的文件夹里
    /// <appSettings> 
    /// <add key="DAL" value="Dong.DAL" /> (这里的命名空间根据实际情况更改为自己项目的命名空间)
    /// </appSettings> 
    /// </summary>
    public abstract class DataAccess
    {
        private static readonly string AssemblyPath = ConfigurationManager.AppSettings["DAL"];
        /// <summary>
        /// 创建对象或从缓存获取
        /// </summary>
        public static object CreateObject(string AssemblyPath, string ClassNamespace)
        {
            object objType = DataCache.GetCache(ClassNamespace);//从缓存读取
            if (objType == null)
            {
                try
                {
                    //Assembly.Load(程序集名称)加载程序集，拿到程序集后，通过CreateInstance(类的完整命名空间)创建程序集中某个类实例（个体）
                    objType = Assembly.Load(AssemblyPath).CreateInstance(ClassNamespace);//反射创建
                    DataCache.SetCache(ClassNamespace, objType);// 写入缓存
                }
                catch
                { }
            }
            return objType;
        }

        /// <summary>
        /// 创建Inventory数据层接口。
        /// </summary>
        public static Dong.IDAL.IInventory CreateInventory()
        {

            string ClassNamespace = AssemblyPath + ".Inventory";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (IInventory)objType;
        }

        /// <summary>
        /// 创建OutboundRecord数据层接口。
        /// </summary>
        public static Dong.IDAL.IOutboundRecord CreateOutboundRecord()
        {
            string ClassNamespace = AssemblyPath + ".OutboundRecord";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IOutboundRecord)objType;
        }

        /// <summary>
        /// 创建Product数据层接口。
        /// </summary>
        public static Dong.IDAL.IProduct CreateProduct()
        {

            string ClassNamespace = AssemblyPath + ".Product";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IProduct)objType;
        }


        /// <summary>
        /// 创建StorageRecord数据层接口。
        /// </summary>
        public static Dong.IDAL.IStorageRecord CreateStorageRecord()
        {
            // AssemblyPath程序集的路径，AssemblyPath + ".StorageRecord"完整的命名空间
            string ClassNamespace = AssemblyPath + ".StorageRecord";
            // CreateObject()在此处返回的object，其实是DAL,Dong.DAL.StoreageRecord,由于Dong.DAL.StoreageRecord实现了Dong.IDAL.IStoreageRecord
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IStorageRecord)objType;
        }


        /// <summary>
        /// 创建Store数据层接口。
        /// </summary>
        public static Dong.IDAL.IStore CreateStore()
        {

            string ClassNamespace = AssemblyPath + ".Store";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IStore)objType;
        }


        /// <summary>
        /// 创建StoreArea数据层接口。
        /// </summary>
        public static Dong.IDAL.IStoreArea CreateStoreArea()
        {

            string ClassNamespace = AssemblyPath + ".StoreArea";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IStoreArea)objType;
        }


        /// <summary>
        /// 创建User数据层接口。
        /// </summary>
        public static Dong.IDAL.IUser CreateUser()
        {

            string ClassNamespace = AssemblyPath + ".User";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IUser)objType;
        }


        /// <summary>
        /// 创建UserType数据层接口。
        /// </summary>
        public static Dong.IDAL.IUserType CreateUserType()
        {

            string ClassNamespace = AssemblyPath + ".UserType";
            object objType = CreateObject(AssemblyPath, ClassNamespace);
            return (Dong.IDAL.IUserType)objType;
        }

    }
}